package com.peas.platform.module.icims.base.approve.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.peas.platform.common.core.dto.BaseDto;
import com.peas.platform.framework.mybatisplus.domain.BaseDomain;
import com.peas.platform.framework.mybatisplus.po.BasePo;
import com.peas.platform.framework.mybatisplus.service.BaseServiceImpl;
import com.peas.platform.module.icims.base.approve.domain.ApproveConditionDomain;
import com.peas.platform.module.icims.base.approve.dto.ApproveConditionDto;
import com.peas.platform.module.icims.base.approve.dto.ApproveDto;
import com.peas.platform.module.icims.base.approve.enums.ApproveBusiness;
import com.peas.platform.module.icims.base.approve.enums.ApproveType;
import com.peas.platform.module.icims.base.approve.service.ApproveConditionService;
import com.peas.platform.module.icims.base.approve.service.ApproveService;
import com.peas.platform.module.icims.base.sys.enums.CommonEnableDisable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Service
public class ApproveConditionServiceImpl extends BaseServiceImpl<ApproveConditionDto> implements ApproveConditionService {

    @Resource
    private ApproveConditionDomain approveConditionDomain;
    @Resource
    private ApproveService approveService;

    @Override
    public BaseDomain<ApproveConditionDto, ? extends BasePo> getDomain() {
        return approveConditionDomain;
    }

    @Override
    public Map<ApproveDto, List<ApproveConditionDto>> queryApproveConditionByBusiness(ApproveBusiness ab) {
        ApproveDto c = new ApproveDto();
        c.setApproveType(ApproveType.conditional.getKey());
        c.setModule(ab.getModule().getKey());
        c.setBusiness(ab.getKey());
        c.setState(CommonEnableDisable.enabled.getKey());
        List<ApproveDto> list = approveService.list(c);
        if (CollUtil.isEmpty(list)) {
            return MapUtil.empty();
        }
        List<Long> ids = list.stream().map(BaseDto::getId).collect(Collectors.toList());
        List<ApproveConditionDto> conditions = this.listByField("approveId", ids);
        Map<ApproveDto, List<ApproveConditionDto>> map = new ConcurrentHashMap<>();
        list.forEach(key -> {
            List<ApproveConditionDto> value = conditions.stream().filter(approveConditionDto -> key.getId().equals(approveConditionDto.getApproveId())).collect(Collectors.toList());
            map.put(key, value);
        });
        return map;
    }

}
